package Hot100.BackTracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class partition {
    public static void main(String[] args) {
        String s = "aab";
        System.out.println(new partition().partition(s));
    }
    List<List<String>> result = new ArrayList<>();
    LinkedList<String> temp_result = new LinkedList<>();

    public List<List<String>> partition(String s) {
       backTracking(s,0);
        return result;
    }
    public void backTracking(String s,int startIndex){
        if(startIndex >= s.length()){
            result.add(new ArrayList<>(temp_result));
            return;
        }
        for (int i = startIndex; i < s.length() ; i++) {
            if(isValid(s,startIndex,i)){
                String temp = s.substring(startIndex,i+1);
                temp_result.add(temp);
            }else {
                continue;
            }
            backTracking(s,i + 1);
            temp_result.removeLast();
        }
    }
    public boolean isValid(String path,int startIndex,int end){
        int left = startIndex, right = end;
        while (right > left){
            if(path.charAt(left) != path.charAt(right))
                return false;
            left++;
            right--;
        }
        return true;
    }
}
